﻿using Castle.Facilities.NHibernateIntegration;
using Castle.Windsor;
using NHibernate;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Model.Common
{
    /// <summary>
    /// NHibernate Session工厂
    /// </summary>
    public class NHSessionFactory
    {
        #region 构造器
        static NHSessionFactory()
        {
            NHSessionFactory.Initialize();
        }
        #endregion

        #region 属性
        /// <summary>
        /// 当前Casstle Session管理器
        /// </summary>
        public static ISessionManager SessionManager
        {
            get;
            private set;
        }

        /// <summary>
        /// 是否已经初始化
        /// </summary>
        private static bool IsInitialize
        {
            get;
            set;
        }
        #endregion

        #region 方法
        /// <summary>
        /// 打开一个有状态Session nh.facility.default
        /// </summary>
        /// <returns>ISession</returns>
        public static ISession OpenSession()
        {
            return NHSessionFactory.OpenSession(null);
        }

        /// <summary>
        /// 打开一有状态Session
        /// </summary>
        /// <param name="dbConfigAlias">数据库别名</param>
        /// <returns>ISession</returns>
        public static ISession OpenSession(string dbConfigAlias)
        {
            ISessionFactoryResolver sessionFactoryResolver = IocHelper.Container.Resolve<ISessionFactoryResolver>();
            ISessionFactory sessionFactory = (dbConfigAlias != null) ?
                sessionFactoryResolver.GetSessionFactory(dbConfigAlias) :
                sessionFactoryResolver.GetSessionFactory("nh.facility.default");

            ISession session = sessionFactory.OpenSession();

            return session;
        }

        /// <summary>
        /// 打开一个无状态Session
        /// </summary>
        /// <returns>IStatelessSession</returns>
        public static IStatelessSession OpenStatelessSession()
        {
            return NHSessionFactory.OpenStatelessSession(null);
        }

        /// <summary>
        /// 打开一个无状态Session
        /// </summary>
        /// <param name="dbConfigAlias">数据库别名</param>
        /// <returns>IStatelessSession</returns>
        public static IStatelessSession OpenStatelessSession(string dbConfigAlias)
        {
            ISessionFactoryResolver sessionFactoryResolver = IocHelper.Container.Resolve<ISessionFactoryResolver>();
            ISessionFactory sessionFactory = (dbConfigAlias != null) ?
                sessionFactoryResolver.GetSessionFactory(dbConfigAlias) :
                sessionFactoryResolver.GetSessionFactory("nh.facility.default");

            // 初始化无状态Session
            IStatelessSession statelessSession = sessionFactory.OpenStatelessSession();

            return statelessSession;
        }

        /// <summary>
        /// 初始化工厂
        /// </summary>
        private static void Initialize()
        {
            if (!NHSessionFactory.IsInitialize)
            {
                lock (typeof(NHSessionFactory))
                {
                    if (!NHSessionFactory.IsInitialize)
                    {
                        if (!IocHelper.IsInitialized)
                        {
                            IWindsorContainer container = new IocContainer();
                            IocHelper.Initialize(container);
                            NHSessionFactory.SessionManager = IocHelper.Container.Resolve<ISessionManager>();
                        }

                        NHSessionFactory.IsInitialize = true;
                    }
                }
            }
        }
        #endregion
    }
}
